package cn.huijielong.modules.pub.controller;

import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.Page;

import cn.huijielong.common.utils.PageUtils;
import cn.huijielong.common.utils.Query;
import cn.huijielong.common.utils.RD;
import cn.huijielong.modules.pub.entity.LocationEntity;
import cn.huijielong.modules.pub.service.LocationService;
import cn.huijielong.modules.sys.controller.AbstractController;




/**
 * 取货地址
 * 
 * @author xietong
 * @email xietong@gmail.com
 * @date 2019-09-21 21:00:53
 */
@RestController
@RequestMapping("/pub/location")
public class LocationController  extends AbstractController{
	@Autowired
	private LocationService locationService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	public RD list(@RequestParam Map<String, Object> params){
	    logger.info("从{}发送请求，参数：{}",getHeader("referer"),params.toString());
	    Long merchantId=this.getMerchant().getMerchantId();
		//查询列表数据
        Query query = Query.createRequest(params);
        query.addParam("merchantId", merchantId);
		Page<LocationEntity> locationList = locationService.queryList(query);		
		PageUtils pageUtil = new PageUtils(locationList, locationList.getTotal(), query.getLimit(), query.getPage());
		
		return RD.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{locationId}")
	public RD info(@PathVariable("locationId") Long locationId){
		LocationEntity location = locationService.queryObject(locationId);
		return RD.ok().put("location", location);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public RD save(@RequestBody LocationEntity location){
		Long merchantId=this.getMerchant().getMerchantId();
		location.setMerchantId(merchantId);
		if(location.getLocationId()==null) {
			locationService.save(location);
		}else {
			locationService.update(location);
		}
		
		return RD.ok().put("locationId", location.getLocationId());
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	public RD update(@RequestBody LocationEntity location){
		locationService.update(location);
		return RD.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public RD delete(){
		Long locationId=Query.createRequest().getLong("locationId");
		locationService.delete(locationId);
		return RD.ok();
	}
	
}
